package com.xtt._04_Apache.commons_collections;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.Predicate;
import org.apache.commons.collections4.Transformer;
import org.apache.commons.collections4.TransformerUtils;
import org.junit.Test;

import java.util.*;

/**
 * @Author xtt.
 * @Description: 对Java标准集合的扩展工具
 * @Date:Create in 2017/9/27 0027 下午 22:36
 * @Modify By:
 */
public class UtilitiesTest {

    /**
     * java Collections
     */
    @Test
    public void testCollections() {
        //addAll
        ArrayList<String> list = new ArrayList<>();
        boolean b = Collections.addAll(list, "1", "2","1","2","3");
        System.out.println(list);//[1, 2, 1, 2, 3]
        //binarySearch
        int index = Collections.binarySearch(list, "2");//采用二分法找出第一个匹配的值的下标index
        System.out.println(index);//3 ==>注意不是1
        //copy
//        ArrayList<String> copy = new ArrayList<>();

//        Collections.copy(copy,list);//Source does not fit in dest
        //copy.size需要大于等于list.size;
        ArrayList copy = new ArrayList(Arrays.asList(new Object[list.size()]));
        Collections.copy(copy,list);
        //==>不如直接new
        ArrayList<String> list1 = new ArrayList<>(list);

        //fill
        Collections.fill(list1,"5");
        System.out.println(list1);//[5, 5, 5, 5, 5]
        //frequency 重复次数
        int frequency = Collections.frequency(list, "2");
        System.out.println(frequency);//2
        //disjoint 没有相同元素true
        boolean disjoint = Collections.disjoint(list, list1);
        System.out.println(disjoint);//true
        //sort 排序
        List<Integer> integers = Arrays.asList(10, 95, 2, 378, 1, 6);
        Collections.sort(integers);
        System.out.println(integers);//[1, 2, 6, 10, 95, 378]
        //revese 反转
        Collections.reverse(integers);
        System.out.println(integers);//[378, 95, 10, 6, 2, 1]
        //rotate 循环向右位移n个位置
        Collections.rotate(integers, 2);
        System.out.println(integers);//[2, 1, 378, 95, 10, 6]
        //swap 交换指定两个位置元素
        Collections.swap(integers,1,3);//[2, 95, 378, 1, 10, 6]
        System.out.println(integers);
        //shuffle 随机排序
        Collections.shuffle(integers);
        System.out.println(integers);//[6, 2, 378, 10, 1, 95]
        //min
        Collections.min(integers);
        //max
        Collections.max(integers);
        //replaceAll
        Collections.replaceAll(integers,10,20);
        System.out.println(integers);//[6, 2, 378, 20, 1, 95]

    }
    /**
     * CollectionUtils
     */
    @Test
    public void testCollectionUtils() {
        List<String> list = new ArrayList<>();
        //addAll
        String[] strs = {"1","2","3","4"};
        CollectionUtils.addAll(list,strs);
        System.out.println(list);
        //addIgnoreNull
        CollectionUtils.addIgnoreNull(list,null);
        System.out.println(list);
        //collate 合并
        List<String> list2 = Arrays.asList("7", "2", "5", "6", "1");
        List<String> collate = CollectionUtils.collate(list, list2);
        System.out.println(collate);//[1, 2, 3, 4, 7, 2, 5, 6, 1]
        //collect 将输入集合经过转换后添加到输出集合
        List<String> out = new ArrayList<>();
        Predicate<String> predicate = val->"2".equals(val);
        Transformer<String, Boolean> stringBooleanTransformer = TransformerUtils.asTransformer(predicate);
        Collection<Boolean> collect = CollectionUtils.collect(list, stringBooleanTransformer);
        System.out.println(collect);//[false, true, false, false]
        //containsAll
        boolean b = CollectionUtils.containsAll(list, list2);
        System.out.println(b);//false
        boolean b1 = CollectionUtils.containsAny(list, list2);
        System.out.println(b1);//true
        //disjunction 差集
        System.out.println(list);//[1, 2, 3, 4]
        System.out.println(list2);//[7, 2, 5, 6, 1]
        Collection<String> disjunction = CollectionUtils.disjunction(list, list2);
        System.out.println(disjunction);//[3, 4, 5, 6, 7]
        //...
    }
    /**
     * ListUtils
     */
    public void testListUtils() {

    }

    /**
     * MapUtils
     */
    public void testMapUtils() {

    }

    /**
     * MultiMapUtils
     */
    public void testMultiMapUtils() {

    }

    /**
     * SetUtils
     */
    public void testSetUtils() {

    }
    /**
     * QueueUtils
     */
    public void testQueueUtils() {

    }
    /**
     * SplitMapUtils
     */
    public void testSplitMapUtils() {

    }

    /**
     *TransformerUtils
     */
    public void testTransformerUtils() {

    }

    /**
     * TriUtils
     */
    public void testTrieUtils() {

    }
    /**
     * BagUtils
     */
    public void testBagUtils() {

    }

    /**
     * ClosureUtils
     */
    public void testClosureUtils() {

    }
    /**
     * IteratorUtils
     */
    public void testIteratorUtils() {

    }
}
